package com.treasure.hunt.popup

import android.content.Context
import android.view.View
import android.view.animation.Animation
import com.blankj.utilcode.util.ToastUtils
import com.module.lemlin.base.loadCenterImage
import com.treasure.hunt.R
import com.treasure.hunt.app.AppConstance
import kotlinx.android.synthetic.main.popup_dialog_card.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.ScaleConfig

class DialogCardPopup(
    context: Context,
    val onConfirmReceive: (view: View) -> Unit,
    val onConfirmGive: (view: View) -> Unit
) : BasePopupWindow(context) {

    init {
        setBlurBackgroundEnable(true)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_card)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogCardReceive.setOnClickListener {
            if (schemeType == "2") {
                onConfirmReceive(it)
            } else if (schemeType == "3") {
                onConfirmGive(it)
            } else {
                ToastUtils.showShort("分享类型错误")
            }
            dismiss()
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withScale(ScaleConfig.CENTER)
            .toDismiss()
    }

    fun setFillImage(image: String): DialogCardPopup {
        val isHttps: Boolean = image.substring(0, 5).contains("http") ||
                image.substring(0, 5).contains("https")
        contentView.ivDialogCardImage.loadCenterImage("${if (isHttps) "" else AppConstance.URL_IMAGE}${image}")
        return this
    }

    private var schemeType: String? = null

    fun setSchemeType(schemeType: String): DialogCardPopup {
        this.schemeType = schemeType
        if (schemeType == "2") {
            contentView.tvDialogCardReceive.text = "赠送"
        } else if (schemeType == "3") {
            contentView.tvDialogCardReceive.text = "领取"
        }
        return this
    }

}